<?php
if(!file_exists('config.php')) header('location:install/index.php');
require('config.php');
require('functions.php');
require('facebooksdk/src/facebook.php');
// cleanup POST variable
if(!empty($_POST)){
	$arrExcludeKeys = array('password');
	foreach($_POST as $key => $val){
		if(!is_array($val) && !in_array($key, $arrExcludeKeys)){
			$_POST[$key] = cleanValue($val);
		}
	}
}
$route = getRoute();
?>
<!DOCTYPE html>
<html lang="en">
  <head>
	    <!-- Meta, title, CSS, favicons, etc. -->
	    <meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="description" content="CyberSlave by thaodx@gmail.com.">
		<meta name="keywords" content="CyberSlave, facebook, page manager">
		<meta name="author" content="thaodx@gmail.com">

		<title>CyberSlave - Ultimate Facebook Viral Solution</title>
		<!-- Latest compiled and minified CSS -->
		<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
		<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
		<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
		<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
		<link rel="stylesheet" href="css/bootstrap.min.css">
		<link rel="stylesheet" href="css/bootswatch.less.css">
		<!-- Optional theme -->
		<!-- <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css"> -->
		<!-- Latest compiled and minified JavaScript -->
		<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
		<style type="text/css">
		body {
			padding-top: 70px;
		}
		#modal-body{
			max-height: 400px;
			overflow: auto;
		}
		.smallfont{
			font-size:11px !important;
			font-family: courier;
		}
		.extract-detail{
			cursor: pointer;
		}
		.tbl-list-post{
			font-size:11px; font-family: tahoma;
		}
		      /* Set the fixed height of the footer here */
		#footer {
			height: 60px;
		}
		#footer {
			background-color: #f5f5f5;
		}
		.data-table{
			max-height: 400px;
			overflow: auto;
		}
		/* Lastly, apply responsive CSS fixes as necessary */
		@media (max-width: 767px) {
			#footer {
				margin-left: -20px;
				margin-right: -20px;
				padding-left: 20px;
				padding-right: 20px;
			}
		}
		.container .credit {
			margin: 20px 0;
		}
		.tbl-list-post tbody tr td{
			vertical-align: middle;
		}
		.dsb{
			font-size: 32px;
			/*font-weight: bold;*/
		}
		.dsb .glyphicon{
			font-size: 75px;
		}
		</style>
		<div id="fb-root"></div>
		<script type="text/javascript">
		var BASE_URL = '//<?php echo $_SERVER['HTTP_HOST'] ?>';
		  window.fbAsyncInit = function() {
			// init the FB JS SDK
			FB.init({
			  appId      : '<?php echo $fbconfig['appId'] ?>',   // App ID from the app dashboard
			  status     : true,                                 // Check Facebook Login status
			  xfbml      : true,                                 // Look for social plugins on the page
			  version    : '<?php echo str_replace('/', '', _FB_API_VERSION); ?>'
			});

			// Additional initialization code such as adding Event Listeners goes here
			
		  };

		  // Load the SDK asynchronously
		  (function(d, s, id){
	         var js, fjs = d.getElementsByTagName(s)[0];
	         if (d.getElementById(id)) {return;}
	         js = d.createElement(s); js.id = id;
	         js.src = "//connect.facebook.net/en_US/sdk.js";
	         fjs.parentNode.insertBefore(js, fjs);
	       }(document, 'script', 'facebook-jssdk'));
			function fbLogin(){
				   var fbloginHtml = $("#btn-fblogin").html();
				   $("#btn-fblogin").html('<img src="/images/loading.gif" height="16" />');
				 FB.login(function(response) {
				   if (response.status == 'connected' && typeof response.authResponse.accessToken != 'undefined') {
						$.ajax({
							url: BASE_URL + '/<?php echo route('ajax') ?>',
							type: 'POST',
							dataType: 'json',
							data: {token: response.authResponse.accessToken, act: 'fblogin'},
							success: function(response){
							  if(typeof response.fbreg != 'undefined' && response.fbreg == 'success'){
							  	location.href = '<?php echo route('index') ?>';
							  	return;
							  }
							  for(uid in response){
							  	$('.user-list').append('<li class="list-group-item"><input id="user_opt_' + uid + '" value="' + uid + '" type="radio" name="useraccount"> <label for="user_opt_' + uid + '">' + response[uid].user_email + '</label></li>');
							  }
							  $('#login-modal').modal({
							  	backdrop: 'static',
							  	keyboard: false,
							  });
							  $("#btn-fblogin").html(fbloginHtml);
							}
						});
				   } else {
					 alert('You\'ve cancelled login or did not fully authorize.');
					 $("#btn-fblogin").html(fbloginHtml);
				   }
				}, {scope: 'email', response_type: 'code'});
			}
			function clearModal(){
				$('.modal-body').html('');
				$('.modal-footer').html('');
				$('.modal-title').html('');
			}
			
			function removeTemplate(tid){
				if(confirm('Are you sure want to delete this template?')){
					$.ajax({
						url: '<?php echo route('ajax') ?>',
						dataType: 'json',
						type: 'POST',
						data: {act:'delete-template', tid:tid},
						success: function(rs){
							alert('Template deleted successful.');
							$('#tpl_' + tid).remove();
						},
						error: function(xhr, status, err){
							alert("Failed to delete template\n" + xhr.responseText);
						}
					});

				}
			}
			</script>
	</head>
	<body>
		<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
		  <div class="container-fluid">
		    <!-- Brand and toggle get grouped for better mobile display -->
		    <div class="navbar-header">
		      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
		        <span class="sr-only">Toggle navigation</span>
		        <span class="icon-bar"></span>
		        <span class="icon-bar"></span>
		        <span class="icon-bar"></span>
		      </button>
		      <a class="navbar-brand" href="<?php echo route('index') ?>"><span class="glyphicon glyphicon-home"> </span> Home</a>
		    </div>

		    <!-- Collect the nav links, forms, and other content for toggling -->
		    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
		    <?php if(!isset($userId) || !$userId): ?>
		      <ul class="nav navbar-nav">
		        <li<?php if($route == 'login') echo ' class="active"' ?>><a href="<?php echo route('login') ?>"><span class="glyphicon glyphicon-log-in"> </span> Login</a></li>
		        <li<?php if($route == 'register') echo ' class="active"' ?>><a href="<?php echo route('register') ?>"><span class="glyphicon glyphicon-list-alt"> </span> Register</a></li>
		      </ul>
		  	<?php else: ?>
		  	<?php if(isset($_SESSION['cs_user_authorized']) && $_SESSION['cs_user_authorized'] == 'YES'): ?>
		  	  <ul class="nav navbar-nav">
		      	<li class="dropdown">
		          <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-file"> </span> Pages <b class="caret"></b></a>
		          <ul class="dropdown-menu">
		            <li><a href="<?php echo route('page') ?>"><span class="glyphicon glyphicon-list-alt"> </span> List post</a></li>
		            <li><a href="<?php echo route('bulkadd') ?>"><span class="glyphicon glyphicon-flash"> </span> Bulk add</a></li>
		          </ul>
		        </li>
		      	<li class="dropdown">
		          <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-folder-open"> </span> Groups <b class="caret"></b></a>
		          <ul class="dropdown-menu">
		            <li><a href="<?php echo route('group') ?>"><span class="glyphicon glyphicon-list-alt"> </span> List post</a></li>
		            <li><a href="<?php echo route('group-bulkadd') ?>"><span class="glyphicon glyphicon-flash"> </span> Bulk add</a></li>
		          </ul>
		        </li>
		      	<li>
		          <a href="<?php echo route('like') ?>"><span class="glyphicon glyphicon-thumbs-up"> </span> Mass FB Like</a>
		        </li>
		      </ul>
		  <?php endif; ?>
		      <ul class="nav navbar-nav navbar-right">
		      	<?php if(isset($_SESSION['cs_user_fb_data']['username'])): ?>
		      	<li><a href="javascript:void(0)" style="padding-top:2px; padding-bottom: 2px;"><img class="img-circle" src="<?php echo getFbAvatar($_SESSION['cs_user_fb_data']['username']) ?>" width="46" /> Hello <?php echo $_SESSION['cs_user_fb_data']['name'] ?></a></li>
		      	<?php endif; ?>
		      	<li class="dropdown">
		          <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-wrench"> </span> Settings <b class="caret"></b></a>
		          <ul class="dropdown-menu">
		            <li><a href="<?php echo route('settings') ?>">Account settings</a></li>
		            <li><a title="Re-authenticate to get latest data from your Facebook account" href="<?php echo route('auth') ?>">Re-authenticate</a></li>
		          </ul>
		        </li>
		        <?php if(isAdmin()): ?>
			      	<li class="dropdown">
			          <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-certificate"> </span> Admin <b class="caret"></b></a>
			          <ul class="dropdown-menu">
			            <li><a href="<?php echo route('users') ?>">Manage users</a></li>
			          </ul>
			        </li>
		    	<?php endif; ?>
		      	<li class="divider"></li>
		      	<li><a href="<?php echo route('logout') ?>"><span class="glyphicon glyphicon-log-out"> </span> Logout</a></li>
		      </ul>
		  	<?php endif; ?>
		    </div><!-- /.navbar-collapse -->
		  </div><!-- /.container-fluid -->
		</nav>
		<div class="container-fluid" style="margin-bottom:30px;">
		<?php if(isset($_SESSION['cs_user_id']) && $_SESSION['cs_user_authorized'] != 'YES'): ?>
    	<div class="alert alert-danger">
    		You need authorized app on Facebook first
    		<a href="/<?php echo route('auth') ?>"><button type="button" class="btn btn-info" > <span class="glyphicon glyphicon-lock"> </span>
		    Authorize now
		  </button></a>
    	</div>
    	<?php endif; ?>
